package com.fenghongzhang.lib_dialog

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghongzhang.lib_dialog.databinding.DialogActivityMainBinding
import java.util.Timer
import java.util.TimerTask
import java.util.Calendar
import java.util.Locale
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Intent
import android.widget.Toast

@Route(path = "/dialog/DialogMainActivity")
class DialogMainActivity : AppCompatActivity() {
    private lateinit var binding: DialogActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        //普通对话框
        binding.normalDialog.setOnClickListener {
            normalDialog()
        }

        //进度条对话框
        binding.progressDialog.setOnClickListener {
            progressDialog()
        }

        //日期选择器的对话框
        binding.datePickerDialog.setOnClickListener {
            showDatePickerDialog()
        }

        //时间选择器对话框
        binding.timePickerDialog.setOnClickListener {
            showTimePickerDialog()
        }

        //警示框
        binding.jingShi.setOnClickListener {
            alertboxDialog()
        }

        //自定义View ，圆和五角星
        binding.yuan.setOnClickListener {
            intent= Intent(this,RoundActivity::class.java)
            startActivity(intent)
        }
    }



    // 普通对话框
    private fun normalDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("普通对话框")
        builder.setMessage("这是一个普通的对话框示例。")

        // 设置确定按钮
        builder.setPositiveButton("确定") { dialog, which ->
            // 点击确定按钮后的操作
            dialog.dismiss()
        }

        // 设置取消按钮
        builder.setNegativeButton("取消") { dialog, which ->
            // 点击取消按钮后的操作
            dialog.dismiss()
        }

        // 创建并显示对话框
        val dialog = builder.create()
        dialog.show()
    }

    // 进度对话框
    private fun progressDialog() {
        // 创建一个垂直的LinearLayout作为容器
        val container = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            setPadding(70, 30, 70, 30)
        }

        // 创建水平进度条
        val progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal).apply {
            max = 100
            progress = 0
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        }

        // 创建百分比文本显示
        val percentText = TextView(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                topMargin = 10
            }
            gravity = Gravity.CENTER
            text = "0%"
            setTextColor(Color.BLACK)
            textSize = 16f
        }

        // 添加进度条和文本到容器
        container.addView(progressBar)
        container.addView(percentText)

        val progressDialog = AlertDialog.Builder(this)
            .setTitle("下载进度")
            .setMessage("正在下载...")
            .setCancelable(true)
            .setView(container)
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
            }
            .create()

        progressDialog.show()

        // 模拟进度更新
        var currentProgress = 0
        val timer = Timer()
        timer.scheduleAtFixedRate(object : TimerTask() {
            override fun run() {
                runOnUiThread {
                    if (currentProgress <= 100 && progressDialog.isShowing) {
                        progressBar.progress = currentProgress
                        percentText.text = "$currentProgress%"
                        currentProgress += 1  // 减慢进度更新速度
                    } else {
                        timer.cancel()
                    }
                }
            }
        }, 0, 50) // 每50毫秒更新一次进度
    }

    // 日期选择器对话框
    private fun showDatePickerDialog() {
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog(
            this,
            { _, selectedYear, selectedMonth, selectedDay ->
                // 格式化日期
                val selectedDate = String.format(
                    Locale.getDefault(),
                    "%d-%02d-%02d",
                    selectedYear,
                    selectedMonth + 1,
                    selectedDay
                )
                Toast.makeText(this, "选择的日期: $selectedDate", Toast.LENGTH_SHORT).show()
            },
            year,
            month,
            dayOfMonth
        )

        // 设置日期范围（可选）
        // datePickerDialog.datePicker.minDate = System.currentTimeMillis() // 设置最小日期为当前日期
        // datePickerDialog.datePicker.maxDate = System.currentTimeMillis() + 31536000000L // 设置最大日期为一年后

        datePickerDialog.show()
    }

    // 时间选择器对话框
    private fun showTimePickerDialog() {
        val calendar = Calendar.getInstance()
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        val timePickerDialog = TimePickerDialog(
            this,
            { _, selectedHour, selectedMinute ->
                // 格式化时间
                val selectedTime = String.format(
                    Locale.getDefault(),
                    "%02d:%02d",
                    selectedHour,
                    selectedMinute
                )
                Toast.makeText(this, "选择的时间: $selectedTime", Toast.LENGTH_SHORT).show()
            },
            hour,
            minute,
            true // 是否使用24小时制
        )

        timePickerDialog.show()
    }

    //警示框
    private fun alertboxDialog() {
        val builder = AlertDialog.Builder(this)
        builder.apply {
            setTitle("⚠️ 警告")  // 添加警告emoji图标
            setMessage("此操作可能会导致数据丢失，是否继续？")  // 更具体的警告信息
//            setIcon(android.R.drawable.ic_dialog_alert)  // 添加警告图标
            
            // 设置确定按钮，使用红色突出警示性
            setPositiveButton("确定") { dialog, _ ->
                Toast.makeText(this@DialogMainActivity, "执行危险操作", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
            }
            
            // 设置取消按钮
            setNegativeButton("取消") { dialog, _ ->
                Toast.makeText(this@DialogMainActivity, "操作已取消", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
            }
            
            // 设置中性按钮，提供更多信息
            setNeutralButton("查看详情") { dialog, _ ->
                Toast.makeText(this@DialogMainActivity, "显示详细信息...", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
            }
            
            // 设置对话框不可取消
            setCancelable(false)
        }
        
        // 创建并显示对话框
        val dialog = builder.create()
        
        // 显示对话框
        dialog.show()
        
        // 可选：修改按钮颜色
        dialog.getButton(AlertDialog.BUTTON_POSITIVE)?.setTextColor(Color.RED)
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE)?.setTextColor(Color.GRAY)
        dialog.getButton(AlertDialog.BUTTON_NEUTRAL)?.setTextColor(Color.BLUE)
    }
}